package com.itheima.galleryview;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.method.Touch;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itheima.galleryview.view.BannerScroller;
import com.itheima.galleryview.view.ViewPagerTransform;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ViewPagerAct extends AppCompatActivity {

    private ViewPagerTransform viewPager;
    private int count = 8;
    private LinearLayout pointLy;
    private TextView txtIndex;
    Handler mHandler;

    int index;
    int s;
    boolean onTouch;
    private List<Integer> bannerPicList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);

        viewPager = (ViewPagerTransform) findViewById(R.id.viewpager);
        pointLy = (LinearLayout) findViewById(R.id.banner_point_ly);
        txtIndex = (TextView) findViewById(R.id.txt_index);
        initPoint();
        // 设置页面间距
        viewPager.setPageMargin(30);
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return Integer.MAX_VALUE;
            }

            public int getStartPageIndex() {
                int index = getCount() / 2;
                int remainder = index % bannerPicList.size();
                index = index - remainder;
                return index;
            }

            private int getBannerIndexOfPosition(int position) {
                return position % bannerPicList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }


            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = View.inflate(container.getContext(), R.layout.widget_gallery_view, null);
//                view.setLayoutParams(getPageLayoutParams());
                ImageView iv = (ImageView) view.findViewById(R.id.headRIV);
                iv.setImageResource(bannerPicList.get(getBannerIndexOfPosition(position)));
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            int lastPosition;


            @Override
            public void onPageSelected(int position) {
                if (onScroll) {
                    txtIndex.setText("" + position);
                    index = position % bannerPicList.size();
                    // 设置当前页面选中
                    pointLy.getChildAt(index).setSelected(true);
                    // 设置前一页不选中
                    pointLy.getChildAt(lastPosition).setSelected(false);
                    lastPosition = index;
                    onScroll = false;
                }

//                // 页面被选中
//                if (index == 2 || index == count - 2) {
//                    // 设置当前页面选中
//                    pointLy.getChildAt(0).setSelected(true);
//                    if (lastPosition >= 0 && lastPosition <= count - 5) {
//                        // 设置前一页不选中
//                        pointLy.getChildAt(lastPosition).setSelected(false);
//                    }
//                } else if (index == 1 || index == count - 3) {
//                    pointLy.getChildAt(count - 5).setSelected(true);
//                    if (lastPosition >= 0 && lastPosition <= count - 5) {
//                        // 设置前一页不选中
//                        pointLy.getChildAt(lastPosition).setSelected(false);
//                    }
//                } else if (index > 2 && index < count - 3) {
//                    pointLy.getChildAt(index - 2).setSelected(true);
//                    // 设置前一页不选中
//                    pointLy.getChildAt(lastPosition).setSelected(false);
//                }
//                // 替换位置
//                lastPosition = index - 2;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == 1) {
                    s = state;
                }
                if (state == 2) {
                    if (s == 1) {
                        s = state;
                        onScroll = true;
                    }
                }
                if (state == 0) {
                    if (s == 2) {
//                        if (index == count - 2) {
//                            viewPager.setCurrentItem(2, false);
//                        }
//                        if (index == 1) {
//                            viewPager.setCurrentItem(count - 3, false);
//                        }
                        s = 0;
                        return;
                    }
                }

            }
        });
        viewPager.setCurrentItem(Integer.MAX_VALUE/4-(Integer.MAX_VALUE%4));
        pointLy.getChildAt(0).setSelected(true);

        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        onTouch = true;
                        time = 0;
                        break;
                    case MotionEvent.ACTION_UP:
                        onTouch = false;
                        break;
                }
                return false;
            }
        });


        initHandler();

        //设置滑动速度
//        try {
//            Field mScroller = ViewPager.class.getDeclaredField("mScroller");
//            mScroller.setAccessible(true);
//            BannerScroller scroller = new BannerScroller(viewPager.getContext(), new AccelerateInterpolator());
//            scroller.setDuration(400);
//            mScroller.set(viewPager, scroller);
//        } catch (NoSuchFieldException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        }

    }

    int time;
    boolean onScroll;
    private void initHandler() {
        mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                time += 1000;
                if (!onTouch && time == 4000) {
                    int currentPosition = viewPager.getCurrentItem();
                    onScroll = true;
                    viewPager.setCurrentItem(currentPosition + 1);
                    time = 0;
                }
                // 一直给自己发消息
                mHandler.postDelayed(this, 1000);
            }
        }, 1000);

    }

    private void initPoint() {
        bannerPicList = new ArrayList<>();
        bannerPicList.add(R.mipmap.demo_img1);
        bannerPicList.add(R.mipmap.demo_img2);
        bannerPicList.add(R.mipmap.a1);
        bannerPicList.add(R.mipmap.head);
        for (int i = 0; i < bannerPicList.size(); i++) {
            // 制作底部小圆点
            ImageView pointImage = new ImageView(this);
            pointImage.setImageResource(R.drawable.banner_point);

            // 设置小圆点的布局参数
            int PointSize = getResources().getDimensionPixelSize(R.dimen.point_size);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(PointSize, PointSize);

            params.leftMargin = getResources().getDimensionPixelSize(R.dimen.point_margin);
            pointImage.setSelected(false);
            pointImage.setLayoutParams(params);
            // 添加到容器里
            pointLy.addView(pointImage);
        }

    }



    private ViewPager.LayoutParams getPageLayoutParams() {
        ViewPager.LayoutParams layoutParams = new ViewPager.LayoutParams();
        layoutParams.width = (int) (viewPager.getMeasuredWidth() * 0.85);
        layoutParams.height = viewPager.getMeasuredHeight();
        return layoutParams;
    }


}
